/**
 * VIP会员服务类
 * 
 */
const request = require('../libs/request');

class VipService {

	/**
	 * 获取VIP套餐列表
	 * @param {Object} params 查询参数
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回VIP套餐列表数据
	 */
	static async getPackages(params = {}) {
		const { 
			showLoading = false 
		} = params;

		try {
			const response = await request.get('/v1/vip/packages', {
				showLoading
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取VIP套餐成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取VIP套餐失败'
				};
			}
		} catch (error) {
			console.error('获取VIP套餐失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取VIP套餐失败'
			};
		}
	}

	/**
	 * 获取用户VIP状态
	 * @param {Object} params 查询参数
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回VIP状态数据
	 */
	static async getStatus(params = {}) {
		const { 
			showLoading = false 
		} = params;

		try {
			const response = await request.authGet('/v1/vip/status', {
				showLoading
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取VIP状态成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取VIP状态失败'
				};
			}
		} catch (error) {
			console.error('获取VIP状态失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取VIP状态失败'
			};
		}
	}

	/**
	 * 创建VIP订单
	 * @param {Object} params 创建订单参数
	 * @param {number} params.packageId 套餐ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回创建订单结果
	 */
	static async createOrder(params = {}) {
		const { 
			packageId, 
			showLoading = false 
		} = params;

		if (!packageId) {
			return {
				success: false,
				data: null,
				message: '套餐ID不能为空'
			};
		}

		try {
			const response = await request.authPost('/v1/vip/createOrder', {
				showLoading,
				data: {
					package_id: packageId
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '创建VIP订单成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '创建VIP订单失败'
				};
			}
		} catch (error) {
			console.error('创建VIP订单失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '创建VIP订单失败'
			};
		}
	}

	/**
	 * 支付VIP订单
	 * @param {Object} params 支付参数
	 * @param {number} params.orderId 订单ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回支付结果
	 */
	static async payOrder(params = {}) {
		const { 
			orderId, 
			showLoading = false 
		} = params;

		if (!orderId) {
			return {
				success: false,
				data: null,
				message: '订单ID不能为空'
			};
		}

		try {
			const response = await request.authPost('/v1/vip/payOrder', {
				showLoading,
				data: {
					order_id: orderId
				}
			});

			console.log(response);

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '支付成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '支付失败'
				};
			}
		} catch (error) {
			console.error('支付VIP订单失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '支付失败'
			};
		}
	}

	static async recentOrders(params = {}) {
		const { 
			showLoading = false 
		} = params;

		try {
			const response = await request.authGet('/v1/vip/recentOrders', {
				showLoading
			});

				if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取最近订单记录成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取最近订单记录失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取最近订单记录失败'
			};
		}
	}	
}

module.exports = VipService; 